package com.glb.leetcode;

import java.util.Arrays;

public class C5_Rotate {
  public static void main(String[] args) {
    int[] nums = {-1,-100,3,99};
    // int[] nums = {1,2,3,4,5,6,7};
    System.out.println(Arrays.toString(nums));
    rotate(nums, 2);
    System.out.println(Arrays.toString(nums));
  }

//   public static void rotate(int[] nums, int k) {
//     int n = nums.length;
//     int[] newArr = new int[n];
//     for (int i = 0; i < n; ++i) {
//         newArr[(i + k) % n] = nums[i];
//     }
//     System.arraycopy(newArr, 0, nums, 0, n);
// }


  public static void rotate(int[] nums, int k) {
    if (nums.length < 2) {
      return ;
    }

    // while (k > 0) {
      // k--;
      int length = nums.length;
      int offect = length%k;
      int temp = nums[length-offect];
      for (int i = length - 1; i > 0; i--) {
        nums[i] = nums[i-1];
      }
      nums[offect-1] = temp;
    // }
  }

}
